package com.sunday.common.mq.rocket.brave.study.base.e7_Retry;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.*;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h，18个level
 * 重试的时候是直接跳过 1s 5s 两个界别，直接从10s进行的，reconsumeTimes=4 消息中存在这个等级标识
 */
@Slf4j
public class RetryConsumer {

    public static void main(String[] args) throws Exception {

        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("retry_consumer_group_name");
        consumer.setNamesrvAddr("127.0.0.1:9876");

        consumer.subscribe("TopicTest", "TagA || TagC || TagD");

        // 最大重试次数：消息消费失败后，可被重复投递的最大次数。
        consumer.setMaxReconsumeTimes(2);

        // 重试间隔：消息消费失败后再次被投递给Consumer消费的间隔时间，只在顺序消费中起作用。
//        consumer.setSuspendCurrentQueueTimeMillis(5000);

        consumer.registerMessageListener((MessageListenerConcurrently) (msgs, context) -> {
            log.info("Receive New Messages: {}", msgs);
            // 标记该消息已经被成功消费
//            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            return ConsumeConcurrentlyStatus.RECONSUME_LATER;
        });

        consumer.start();

        log.info("Consumer Started.");
    }

}
